﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;
using System.Collections.Generic;

public partial class UserPlanMonth_UserPlanMonthList : System.Web.UI.Page
{
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
    IBussinessLogic dllMonthPlan = new MonthPlanManager();
    Authentication dllAuthentication = new Authentication();
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    Month_RemindofManager dllMonth_RemindofManager = new Month_RemindofManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    DataTable _powerTable = new DataTable();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    public string TheMonthRemind = "";
    public string PreMonthRemind = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        Page.RegisterStartupScript("getheighandwidth", "<script>document.getElementById('h1h').value=window.screen.availHeight;document.getElementById('h2w').value= window.screen.width ;</script>");
        if (objUserInfo.UserName == "admin")
        {
            this.btnDisplay.Enabled = true;
        }
        else
        {
            this.btnDisplay.Enabled = false;
        }
        //判断是否为经理，非经理锁定部门选择框
        this.drpDepartment.Enabled = false;
        DataTable dt = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), new object[] { objUserInfo.UserName });
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["IsDepartmentManager"].ToString() != "1")
            {
                this.btnDel.Visible = false;
                this.btnModifyContinue.Visible = false;
                this.btnModifyMonth.Visible = false;
                this.btnModifyPrevMonth.Visible = false;

            }
        }
        if (objUserInfo.UserName == "admin")
        {
            this.drpDepartment.Enabled = true;
        }
        if (!IsPostBack)
        {
            //绑定本月计划
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpUserInfo);
            bindDropDownList(this.drpQuarterDate);
            bindDropDownList(this.drpMonthDate);
            bindGridView(this.gdvPreMonthList);
            bindGridView(this.gdvMonthList);
            bindGridView(this.gdvPreNoCompeleteMonthPlan);
            bindGridView(this.gdvContinueMonthList);
        }
    }




    /// <summary>
    /// 绑定所有的dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        //部门绑定
        if (_drp.Equals(this.drpDepartment))
        {
            this.drpDepartment.DataSource = dllDepartment.getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门");
            this.drpDepartment.Items.Insert(0, _li);
            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(_userTable.Rows[0]["DepartmentId"].ToString()));
            }
        }
        if (_drp.Equals(this.drpUserInfo))
        {
            this.drpUserInfo.Items.Clear();
            object[] _value = new object[1];
            _value[0] = this.drpDepartment.SelectedItem.Text;
            DataTable _userInfo = dllUserInfoManager.getUserInfoByDepartmentName(_value);
            if (_userInfo.Rows.Count > 0)
            {
                this.drpUserInfo.DataSource = _userInfo;
                this.drpUserInfo.DataTextField = "RealName";
                this.drpUserInfo.DataValueField = "UserId";
                this.drpUserInfo.DataBind();
                ListItem _selectLi = new ListItem("请选择员工", "0");
                this.drpUserInfo.Items.Insert(0, _selectLi);
                object[] _valueuser = new object[1];
                _valueuser[0] = objUserInfo.UserName;
                DataTable _userTable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _valueuser);
                if (_userTable.Rows.Count > 0)
                {
                    if (Request.Params["User"] != null && Request.Params["User"].ToString().Length != 0)
                    {
                        this.drpUserInfo.SelectedIndex = this.drpUserInfo.Items.IndexOf(this.drpUserInfo.Items.FindByValue(Request.Params["User"].ToString()));
                    }
                    else
                    {
                        this.drpUserInfo.SelectedIndex = this.drpUserInfo.Items.IndexOf(this.drpUserInfo.Items.FindByText(_userTable.Rows[0]["RealName"].ToString()));
                    }
                }
            }
            else
            {
                this.drpUserInfo.Items.Clear();
                ListItem _li = new ListItem("该部门无员工", "0");
                this.drpUserInfo.Items.Add(_li);
            }
        }
        //年份绑定
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            if (Request.Params["Year"] != null && Request.Params["Year"].ToString().Length != 0)
            {
                this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByValue(Request.Params["Year"].ToString()));
            }
        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）
                if (_quartertime.Rows.Count > 0)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    if (Request.Params["Quarter"] != null && Request.Params["Quarter"].ToString().Length != 0)
                    {

                        this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByValue(Request.Params["Quarter"].ToString()));

                    }
                }
                #endregion
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem _li = new ListItem("无年周期", "0");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(_li);
                bindDropDownList(this.drpMonthDate);
            }

        }

        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {
            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count > 0)
                {
                    this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                    if (Request.Params["Month"] != null && Request.Params["Month"].ToString().Length != 0)
                    {
                        this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByValue(Request.Params["Month"].ToString()));
                    }
                }


                #endregion

            }
            else
            {
                ListItem _li = new ListItem("无月周期", "0");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
            }
        }
    }

    //构建上月总结
    DataTable createMonthSummary()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("PreMonthSummary");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MonthSummary");
        _dt.Columns.Add(dc);
        dc = new DataColumn("AttachmentPath");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Id");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["PreMonthSummary"] = "";
        dr["MonthSummary"] = "";
        dr["AttachmentPath"] = "";
        dr["Id"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }

    /// <summary>
    /// 绑定gridview事件
    /// </summary>
    void bindGridView(GridView _gridView)
    {
        if (_gridView.Equals(this.gdvPreMonthList))
        {
            #region 绑定上月计划
            this.gdvPreMonthList.DataSource = new DataTable();
            this.gdvPreMonthList.DataBind();
            //得到用户名
            Model.UserInfo _objUserInfo = new Model.UserInfo();
            object[] _value = new object[3];
            _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _value[1] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
            _value[2] = this.drpDepartment.SelectedItem.Text;
            DataSet _monthPlan = dllUserPlanMonth.getPreUserMonthPlan(_value);
            if (_monthPlan.Tables.Count > 0)
            {
                for (int j = 0; j < _monthPlan.Tables.Count; j++)
                {
                    if (_monthPlan.Tables[j].Rows.Count > 0)
                    {
                        DataView dv = new DataView(_monthPlan.Tables[j]);
                        dv.Sort = "MonthPlanSortId";
                        this.gdvPreMonthList.DataSource = dv;
                        this.gdvPreMonthList.DataBind();
                    }
                }
            }
            if (this.gdvPreMonthList.Rows.Count == 0)
            {
                this.gdvPreMonthList.DataSource = createDataTable();
                this.gdvPreMonthList.DataBind();
            }
            #endregion
            processGridView(_gridView);
            setUploadfile(gdvPreMonthList, "btnDownLoad", "lblAlert");
        }
        //绑定月总结
        if (_gridView.Equals(this.gdvPreMonthList))
        {
            object[] _valueSummary = new object[3];
            _valueSummary[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _valueSummary[1] = this.drpDepartment.SelectedItem.Text;
            _valueSummary[2] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
            DataTable dt = new BLL.UserMonthSummary().get(BLL.UserMonthSummary.field.MonthDate.ToString(), BLL.UserMonthSummary.field.Department.ToString(), _valueSummary);
            if (dt.Rows.Count > 0)
            {
                this.txtMonthSummary.Text = dt.Rows[0]["MonthSummary"].ToString();
                this.txtPreMonthSummary.Text = dt.Rows[0]["PreMonthSummary"].ToString();
                this.prelength.InnerHtml = this.txtPreMonthSummary.Text.Length.ToString();
                this.thelength.InnerHtml = this.txtMonthSummary.Text.Length.ToString();
                this.HMonthSummaryId.Value = dt.Rows[0]["Id"].ToString();
                this.HPreAttachmentPath.Value = dt.Rows[0]["PreAttachmentPath"].ToString();
                this.HAttachmentPath.Value = dt.Rows[0]["AttachmentPath"].ToString();

            }
            else
            {
                this.txtPreMonthSummary.Text = "注：工作总结不少于3条，总字数不少于60个字。";
                this.txtMonthSummary.Text = "注：工作部署不少于3条，总字数不少于60个字。";
                this.prelength.InnerHtml = "0";
                this.thelength.InnerHtml = "0";
                this.HMonthSummaryId.Value = string.Empty;
                this.HPreAttachmentPath.Value = string.Empty;
                this.HAttachmentPath.Value = string.Empty;
            }
            if (this.txtMonthSummary.Text.Length > 0 && this.txtMonthSummary.Text != "注：工作部署不少于3条，总字数不少于60个字。")
            {
                this.btnAddMonthSummary.Text = "修改";
            }
            else
            {
                this.btnAddMonthSummary.Text = "添加";
            }
            if (this.txtPreMonthSummary.Text.Length > 0 && this.txtPreMonthSummary.Text != "注：工作总结不少于3条，总字数不少于60个字。")
            {
                this.btnAddPreMonthSummary.Text = "修改";
            }
            else
            {
                this.btnAddPreMonthSummary.Text = "添加";
            }
            if (this.HPreAttachmentPath.Value.Length > 0)
            {
                this.btnDownPreMonth.Enabled = true;
            }
            else
            {
                this.btnDownPreMonth.Enabled = false;
            }
            if (this.HAttachmentPath.Value.Length > 0)
            {
                this.btnDownMonthSummary.Enabled = true;
            }
            else
            {
                this.btnDownMonthSummary.Enabled = false;
            }
            if (objUserInfo.UserName != "admin")
            {
                bool Isdepartment = false;
                //当前部门并且当前月份
                object[] _value = new object[1];
                _value[0] = objUserInfo.UserName;
                DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _value);
                if (_userTable.Rows.Count > 0)
                {
                    if (_userTable.Rows[0]["DepartmentId"].ToString() == this.drpDepartment.SelectedItem.Text || _userTable.Rows[0]["PartTimeDepartmentId"].ToString() == this.drpDepartment.SelectedItem.Text)
                    {
                        Isdepartment = true;
                    }
                }

                bool IsDate = new BLL.MonthDateManager().IsEditMonthSummary(new object[] { Convert.ToInt32(this.drpMonthDate.SelectedValue) });
                //DataTable _monthtime = dllMonthDateManager.getMonthDate();
                //if (this.drpMonthDate.SelectedItem.Text == _monthtime.Rows[0][3].ToString())
                //{
                //    IsDate = true;

                //}
                if (Isdepartment && IsDate && this.drpUserInfo.SelectedValue == _userTable.Rows[0]["UserId"].ToString())
                {
                    this.btnAddPreMonthSummary.Enabled = true;
                    this.btnAddMonthSummary.Enabled = true;
                    this.btnUploadMonthSummary.Enabled = true;
                    this.btnUploadPreMonthSummary.Enabled = true;
                }
                else
                {
                    this.btnAddPreMonthSummary.Enabled = false;
                    this.btnAddMonthSummary.Enabled = false;
                    this.btnUploadMonthSummary.Enabled = false;
                    this.btnUploadPreMonthSummary.Enabled = false;
                }
                this.btnSaveMonthSummary.Visible = false;
                this.btnSavePreMonthSummary.Visible = false;
                this.btnAddMonthSummary.Visible = true;
                this.btnAddPreMonthSummary.Visible = true;
            }
            //this.txtMonthSummary.Attributes.Add("readonly", "true");
            //this.txtPreMonthSummary.Attributes.Add("readonly", "true");
            this.txtMonthSummary.ReadOnly = true;
            this.txtPreMonthSummary.ReadOnly = true;
        }

        if (_gridView.Equals(this.gdvMonthList))
        {
            #region 绑定本月计划
            this.gdvMonthList.DataSource = new DataTable();
            this.gdvMonthList.DataBind();
            //得到用户名
            Model.UserInfo _objUserInfo = new Model.UserInfo();
            object[] _value = new object[3];
            _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _value[1] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
            _value[2] = this.drpDepartment.SelectedItem.Text;
            DataSet _monthPlan = dllUserPlanMonth.getUserMonthPlan(_value);
            if (_monthPlan.Tables.Count > 0)
            {
                for (int j = 0; j < _monthPlan.Tables.Count; j++)
                {
                    if (_monthPlan.Tables[j].Rows.Count > 0)
                    {
                        DataView dv = new DataView(_monthPlan.Tables[j]);
                        dv.Sort = "MonthPlanSortId";
                        this.gdvMonthList.DataSource = dv;
                        this.gdvMonthList.DataBind();
                    }
                }
            }
            if (this.gdvMonthList.Rows.Count == 0)
            {
                this.gdvMonthList.DataSource = createDataTable();
                this.gdvMonthList.DataBind();
            }

            #endregion
            processGridViewForMonth(_gridView);
            setUploadfile(gdvMonthList, "btndown2", "lblAlert2");
        }
        if (_gridView.Equals(this.gdvPreNoCompeleteMonthPlan))
        {
            #region 绑定上月未完成计划
            this.gdvPreNoCompeleteMonthPlan.DataSource = new DataTable();
            this.gdvPreNoCompeleteMonthPlan.DataBind();
            //得到用户名
            Model.UserInfo _objUserInfo = new Model.UserInfo();
            object[] _value = new object[3];
            _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _value[1] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
            _value[2] = this.drpDepartment.SelectedItem.Text;
            DataSet _monthPlan = dllUserPlanMonth.getPreUserMonthPlanNoComplete(_value);
            if (_monthPlan.Tables.Count > 0)
            {
                for (int j = 0; j < _monthPlan.Tables.Count; j++)
                {
                    if (_monthPlan.Tables[j].Rows.Count > 0)
                    {
                        DataView dv = new DataView(_monthPlan.Tables[j]);
                        dv.Sort = "MonthPlanSortId";
                        this.gdvPreNoCompeleteMonthPlan.DataSource = dv;
                        this.gdvPreNoCompeleteMonthPlan.DataBind();
                    }
                }
            }
            if (this.gdvPreNoCompeleteMonthPlan.Rows.Count == 0)
            {
                this.gdvPreNoCompeleteMonthPlan.DataSource = createDataTable();
                this.gdvPreNoCompeleteMonthPlan.DataBind();
            }

            #endregion
            processGridView(_gridView);
            setUploadfile(gdvPreNoCompeleteMonthPlan, "btndown3", "lblAlert3");
        }
        if (_gridView.Equals(this.gdvContinueMonthList))
        {
            #region 绑定上月持续计划
            this.gdvContinueMonthList.DataSource = new DataTable();
            this.gdvContinueMonthList.DataBind();
            //得到用户名
            Model.UserInfo _objUserInfo = new Model.UserInfo();
            object[] _value = new object[3];
            _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
            _value[1] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
            _value[2] = this.drpDepartment.SelectedItem.Text;
            DataSet _monthPlan = dllUserPlanMonth.getUserMonthPlanContinue(_value);
            if (_monthPlan.Tables.Count > 0)
            {
                for (int j = 0; j < _monthPlan.Tables.Count; j++)
                {
                    if (_monthPlan.Tables[j].Rows.Count > 0)
                    {
                        DataView dv = new DataView(_monthPlan.Tables[j]);
                        dv.Sort = "MonthPlanSortId";
                        this.gdvContinueMonthList.DataSource = dv;
                        this.gdvContinueMonthList.DataBind();
                    }
                }
            }

            if (this.gdvContinueMonthList.Rows.Count == 0)
            {
                this.gdvContinueMonthList.DataSource = createDataTable();
                this.gdvContinueMonthList.DataBind();
            }

            #endregion
            processGridViewForMonth(_gridView);
            setUploadfile(gdvContinueMonthList, "btndown4", "lblAlert4");
        }

    }


    /// <summary>
    /// 处理日期和来源显示以及根据逗号换行
    /// </summary>
    /// <param name="_gridView"></param>
    void processGridView(GridView _gridView)
    {
        for (int i = 0; i < _gridView.Rows.Count; i++)
        {
            wrapByComma(_gridView, i, 7);
            LinkButton lbtn = (LinkButton)_gridView.Rows[i].Cells[2].FindControl("lbtnSource");
            lbtn.Text = _gridView.DataKeys[i]["Source"].ToString();

            if (_gridView.Rows[i].Cells[8].Text != " " && _gridView.Rows[i].Cells[8].Text != "" && _gridView.DataKeys[i]["NoCompleteCount"].ToString() != "")
            {
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 4)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.FromName("#00C0C0");
                }
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 5)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.FromName("#00C000");
                }
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 6)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Yellow;
                }

                //判断2次以上变红为失控状态
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 5 && Convert.ToInt32(_gridView.DataKeys[i]["NoCompleteCount"].ToString()) > 2)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Red;
                }
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 6 && Convert.ToInt32(_gridView.DataKeys[i]["NoCompleteCount"].ToString()) > 2)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Red;
                }
                //失控标红
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 7)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Red;
                }
                //计划调整变灰
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 8)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Gray;
                }
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 1
                    || Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 2
                    || Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 9)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Pink;
                }

            }
            if (_gridView.Rows[i].Cells[9].Text == "&nbsp;" || _gridView.Rows[i].Cells[9].Text == "&amp;nbsp;")
            {
                _gridView.Rows[i].Cells[9].Text = "";
            }
            if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 0)
            {
                _gridView.Rows[i].Cells[8].Text = "";
            }
        }
    }


    /// <summary>
    /// 处理日期和来源显示以及根据逗号换行
    /// </summary>
    /// <param name="_gridView"></param>
    void processGridViewForMonth(GridView _gridView)
    {
        for (int i = 0; i < _gridView.Rows.Count; i++)
        {
            LinkButton lbtn = (LinkButton)_gridView.Rows[i].Cells[2].FindControl("lbtnSource");
            lbtn.Text = _gridView.DataKeys[i]["Source"].ToString();
            //gridview的第六和七行，根据逗号换行
            wrapByComma(_gridView, i, 7);

            if (_gridView.Rows[i].Cells[8].Text != " " && _gridView.Rows[i].Cells[8].Text != "" && _gridView.DataKeys[i]["NoCompleteCount"].ToString() != "")
            {


                //if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) > 2)
                //{
                //    _gridView.Rows[i].Cells[8].Text = "达成共识";
                //}
                //else
                //{
                //    _gridView.Rows[i].Cells[8].Text = "";
                //}
                if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 1
                    || Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 2
                    || Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 9)
                {
                    _gridView.Rows[i].BackColor = System.Drawing.Color.Pink;
                }
            }
            if (_gridView.Rows[i].Cells[9].Text == "&nbsp;" || _gridView.Rows[i].Cells[9].Text == "&amp;nbsp;")
            {
                _gridView.Rows[i].Cells[9].Text = "";
            }
            if (Convert.ToInt32(_gridView.DataKeys[i]["PlanSendState"].ToString()) == 0)
            {
                _gridView.Rows[i].Cells[8].Text = "";
            }
        }
    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");

        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }


    void setUploadfile(GridView objgv, string btnname, string lblname)
    {
        #region
        for (int i = 0; i < objgv.Rows.Count; i++)
        {
            //文件下载，判断如果下载了就显示《下载按钮》 反之 就显示“无下载”
            if (objgv.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)objgv.Rows[i].Cells[12].FindControl(btnname);
                _btn.Visible = false;
                Label _lbl = (Label)objgv.Rows[i].Cells[12].FindControl(lblname);
                _lbl.Visible = true;
                _lbl.Text = "无下载";
            }
        }

        #endregion
    }


    //构建上月计划表结构并添加1行
    DataTable createDataTable()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("MonthPlanSortId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("WorkItem");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Source");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CompleteTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MasterPeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartment");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartmentDuty");
        _dt.Columns.Add(dc);
        dc = new DataColumn("EvaluatePeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PlanSendState");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CheckNote");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Remart");
        _dt.Columns.Add(dc);
        dc = new DataColumn("QuarterPlanId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("AttachmentPath");
        _dt.Columns.Add(dc);
        dc = new DataColumn("NoCompleteCount");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MonthPlanId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PlanStep");
        _dt.Columns.Add(dc);
        dc = new DataColumn("VerifyPeopleOpinion");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["MonthPlanSortId"] = "";
        dr["WorkItem"] = "";
        dr["Source"] = "";
        dr["CompleteTime"] = "";
        dr["MasterPeople"] = "";
        dr["SlaveDepartment"] = "";
        dr["SlaveDepartmentDuty"] = "";
        dr["EvaluatePeople"] = "";
        dr["PlanSendState"] = "0";
        dr["CheckNote"] = "";
        dr["Remart"] = "";
        dr["QuarterPlanId"] = "";
        dr["NoCompleteCount"] = "";
        dr["AttachmentPath"] = "";
        dr["MonthPlanId"] = "";
        dr["PlanStep"] = "";
        dr["VerifyPeopleOpinion"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }



    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindDropDownList(this.drpMonthDate);
    }
    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
        this.btnAddMonthSummary.Visible = true;
        this.btnAddPreMonthSummary.Visible = true;
        this.btnSaveMonthSummary.Visible = false;
        this.btnSavePreMonthSummary.Visible = false;
    }
    /// <summary>
    /// 月周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView(this.gdvPreMonthList);
        bindGridView(this.gdvMonthList);
        bindGridView(this.gdvPreNoCompeleteMonthPlan);
        bindGridView(this.gdvContinueMonthList);
        this.btnAddMonthSummary.Visible = true;
        this.btnAddPreMonthSummary.Visible = true;
        this.btnSaveMonthSummary.Visible = false;
        this.btnSavePreMonthSummary.Visible = false;
    }

    /// <summary>
    /// 部门改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpDepartment.SelectedItem != null && this.drpDepartment.SelectedItem.Text != "请选择部门")
        {
            bindDropDownList(this.drpUserInfo);
        }
        this.btnAddMonthSummary.Visible = true;
        this.btnAddPreMonthSummary.Visible = true;
        this.btnSaveMonthSummary.Visible = false;
        this.btnSavePreMonthSummary.Visible = false;
    }

    /// <summary>
    /// 来源年计划链接事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnSource_Click(object sender, EventArgs e)
    {
        int i = 0;
        LinkButton bt = (LinkButton)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        Control c = (Control)iRow.Parent;
        GridView _gdv = (GridView)c.Parent;
        //因员工月计划的来源字段为QuarterPlanId！
        int _monthPlanId = Convert.ToInt32(_gdv.DataKeys[i]["QuarterPlanId"].ToString());
        if (_monthPlanId == 0)
        {
            //新增的月计划，不需要跳转到指定页面
        }
        else
        {
            //指向周计划中查看部门月计划查看
            string _url = "../Week/MonthPlanDetail.aspx?MonthPlanId=" + _monthPlanId;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=200,width=900,top=200,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        }


    }
    /// 下载附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvPreMonthList.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }

    /// <summary>
    /// 本月下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btndown2_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvMonthList.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
        //Utility.HttpHelp.DownloadFile(path);
    }
    /// <summary>
    /// 上月未完成工作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btndown3_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
        //Utility.HttpHelp.DownloadFile(path);
    }
    /// <summary>
    /// 持续计划下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btndown4_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvContinueMonthList.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
        //Utility.HttpHelp.DownloadFile(path);
    }

    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {
        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();
    }


    /// 添加附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.HMonthSummaryId.Value.Length > 0)
        {
            if (sender.Equals(this.btnUploadMonthSummary))
            {
                //定向到添加本月附件路径页面
                string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + this.HMonthSummaryId.Value + "&PlanType=21&Url=UserPlanMonthList.aspx";
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
            }
            else
            {
                //定向到添加上月附件路径页面
                string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + this.HMonthSummaryId.Value + "&PlanType=22&Url=UserPlanMonthList.aspx";
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
            }
        }
        else
        {
            if (sender.Equals(this.btnUploadMonthSummary))
            {
                Utility.HttpHelp.alert("请先添加本月总结", this.Page);
            }
            else
            {
                Utility.HttpHelp.alert("请先添加上月总结", this.Page);
            }

        }

    }


    /// <summary>
    /// 添加修改月总结事件。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddMonthSummary_Click(object sender, EventArgs e)
    {
        this.btnSaveMonthSummary.Visible = true;
        this.btnAddMonthSummary.Visible = false;
        this.txtMonthSummary.ReadOnly = false;
        if (this.txtMonthSummary.Text == "注：工作部署不少于3条，总字数不少于60个字。")
        {
            this.txtMonthSummary.Text = "";
        }
        //this.lblMonthSummary.Visible = false;
    }
    protected void btnAddPreMonthSummary_Click(object sender, EventArgs e)
    {
        this.btnSavePreMonthSummary.Visible = true;
        this.btnAddPreMonthSummary.Visible = false;
        this.txtPreMonthSummary.ReadOnly = false;
        if (this.txtPreMonthSummary.Text == "注：工作总结不少于3条，总字数不少于60个字。")
        {
            this.txtPreMonthSummary.Text = "";
        }
        //this.lblPreMonthSummary.Visible = false;
    }

    /// <summary>
    /// 员工改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpUserInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpUserInfo != null)
        {
            //评估人才可以判断字数
            object[] _valueUser = new object[1];
            _valueUser[0] = objUserInfo.UserName;
            DataTable dt = new BLL.UserInfoManager().get(UserInfoManager.field.UserName.ToString(), _valueUser);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["RealName"].ToString() == this.drpUserInfo.SelectedItem.Text)
                {
                    this.btnMonthResolve.Enabled = true;
                    this.btnResolve.Enabled = true;
                    this.btnModifyMonth.Enabled = true;
                    this.btnModifyContinue.Enabled = true;
                    this.btnModifyPrevMonth.Enabled = true;
                }
                else
                {
                    this.btnMonthResolve.Enabled = false;
                    this.btnResolve.Enabled = false;
                    this.btnModifyMonth.Enabled = false;
                    this.btnModifyContinue.Enabled = false;
                    this.btnModifyPrevMonth.Enabled = false;
                }
                int _isManager = dllAuthentication.IsManager(objUserInfo.UserName);
                if (_isManager > 0)
                {
                    this.btnMonthResolve.Enabled = true;
                    this.btnResolve.Enabled = true;
                    this.btnModifyMonth.Enabled = true;
                    this.btnModifyContinue.Enabled = true;
                    this.btnModifyPrevMonth.Enabled = true;
                }
            }
            bindGridView(this.gdvPreMonthList);
            bindGridView(this.gdvMonthList);
            bindGridView(this.gdvPreNoCompeleteMonthPlan);
            bindGridView(this.gdvContinueMonthList);
        }
    }


    /// <summary>
    /// 分解到员工周
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnResolve_Click(object sender, EventArgs e)
    {
        object[] _value = new object[1];
        int _monthId = 0;
        int _count = 0;
        BLL.UserPlanMonth dllUserPlanMonth = new UserPlanMonth();

        for (int i = 0; i < this.gdvMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {

                    _monthId = Convert.ToInt32(this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    _value[0] = _monthId;
                    //if (objUserInfo.UserName != "admin")
                    //{
                    _count++;
                    int val = dllUserPlanMonth.IsUserMonthPlanResolve(_value);
                    if (val == 0)
                    {
                        if (dllUserPlanMonth.UserMonthPlanToUserWeekPlan(_value) > 0)
                        {
                            Utility.HttpHelp.alert("分解成功！", this.Page);
                        }
                    }
                    else
                    {
                        if (dllUserPlanMonth.UserMonthPlanToUserWeekPlan(_value) > 0)
                        {
                            Utility.HttpHelp.alert("分解成功！", this.Page);
                        }
                    }
                }

            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择要分解的本月计划Id", this.Page);
        }
    }



    /// <summary>
    /// 修改本月计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyMonth_Click(object sender, EventArgs e)
    {
        BLL.UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
        string department = drpDepartment.SelectedItem.Value;
        object[] _valueMonth = new object[1];
        int _count = 0;
        int _monthId = 0;
        for (int i = 0; i < this.gdvMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    object[] _value = new object[1];
                    _value[0] = _monthId;
                    DataTable _monthPlan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
                    if (_monthPlan.Rows.Count > 0)
                    {
                        object[] _user = new object[1];
                        _user[0] = objUserInfo.UserName;
                        DataTable _usertable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _user);
                        string RealName = "";
                        if (_usertable.Rows.Count > 0)
                        {
                            RealName = _usertable.Rows[0]["RealName"].ToString();
                        }

                        if (objUserInfo.UserName != "admin")
                        {
                            _valueMonth[0] = _monthId;
                            if (_monthPlan.Rows[0]["EvaluatePeople"].ToString() == RealName)
                            {
                                _count++;
                            }
                            else
                            {
                                int val = dllUserPlanMonth.IsUserMonthPlanResolve(_valueMonth);
                                if (val == 0)
                                {
                                    _count++;
                                }
                                else
                                {
                                    Utility.HttpHelp.alert("该月计划已经分解到周中", this.Page);
                                    return;
                                }
                            }
                        }
                        else
                        {
                            _count++;
                        }

                    }
                }
            }
        }
        if (_count == 1)
        {

            //判断是否计划二次评估完成， 评估完成不允许修改。
            object[] _value = new object[1];
            _value[0] = _monthId;
            DataTable monthplan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
            if (monthplan.Rows.Count == 0)
            {
                if (Convert.ToInt32(monthplan.Rows[0]["PlanSendState"]) >= 4)
                {
                    Utility.HttpHelp.alert("计划已二次评估完成，不允许修改！", Page);
                    return;
                }
            }

            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            BLL.UserInfoManager dllUserInfo = new UserInfoManager();
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
            if (_userTable.Rows.Count > 0)
            {
                object[] _username = new object[2];
                _username[0] = _userTable.Rows[0]["RealName"].ToString();
                _username[1] = _monthId;
                DataTable _iSEvaluatePeople = dllUserPlanMonth.get(BLL.UserPlanMonth.field.EvaluatePeople.ToString(), BLL.UserPlanMonth.field.MonthPlanId.ToString(), _username);
                if (_iSEvaluatePeople.Rows.Count == 0 && objUserInfo.UserName != "admin")
                {
                    Utility.HttpHelp.alert("非评估人不能修改计划", Page);
                }
                else
                {
                    string _url = "UserMonthPlanModify.aspx?Id=" + _monthId + "&url=UserPlanMonthList.aspx&MonthDate=" + Utility.HttpHelp.getParamsInt("Id") + "&depart=" + department + "&Year=" + this.drpYear.SelectedValue + "&Quarter=" + this.drpQuarterDate.SelectedValue + "&Month=" + this.drpMonthDate.SelectedValue + "&User=" + this.drpUserInfo.SelectedValue;
                    Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=1000,top=300,left=20,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
                    return;
                }
            }

        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能修改一个月计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个月计划", Page);
            return;
        }
    }

    /// <summary>
    /// 修改上月未完成计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyPrevMonth_Click(object sender, EventArgs e)
    {
        BLL.UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
        string department = drpDepartment.SelectedItem.Value;
        object[] _valueMonth = new object[1];
        int _count = 0;
        int _monthId = 0;
        for (int i = 0; i < this.gdvPreNoCompeleteMonthPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvPreNoCompeleteMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"] != null && this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"].ToString());
                    object[] _value = new object[1];
                    _value[0] = _monthId;
                    DataTable _monthPlan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
                    if (_monthPlan.Rows.Count > 0)
                    {
                        object[] _user = new object[1];
                        _user[0] = objUserInfo.UserName;
                        DataTable _usertable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _user);
                        string RealName = "";
                        if (_usertable.Rows.Count > 0)
                        {
                            RealName = _usertable.Rows[0]["RealName"].ToString();
                        }

                        if (objUserInfo.UserName != "admin")
                        {
                            _valueMonth[0] = _monthId;
                            if (_monthPlan.Rows[0]["EvaluatePeople"].ToString() == RealName)
                            {
                                _count++;
                            }
                            else
                            {
                                int val = dllUserPlanMonth.IsUserMonthPlanResolve(_valueMonth);
                                if (val == 0)
                                {
                                    _count++;
                                }
                                else
                                {
                                    Utility.HttpHelp.alert("该月计划已经分解到周中", this.Page);
                                    return;
                                }
                            }
                        }
                        else
                        {
                            _count++;
                        }
                    }

                }
            }
        }
        if (_count == 1)
        {

            //判断是否计划二次评估完成， 评估完成不允许修改。
            object[] _value = new object[1];
            _value[0] = _monthId;
            DataTable monthplan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
            if (monthplan.Rows.Count == 0)
            {
                if (Convert.ToInt32(monthplan.Rows[0]["PlanSendState"]) >= 4)
                {
                    Utility.HttpHelp.alert("计划已二次评估完成，不允许修改！", Page);
                    return;
                }
            }

            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            BLL.UserInfoManager dllUserInfo = new UserInfoManager();
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
            if (_userTable.Rows.Count > 0)
            {
                object[] _username = new object[2];
                _username[0] = _userTable.Rows[0]["RealName"].ToString();
                _username[1] = _monthId;
                DataTable _iSEvaluatePeople = dllUserPlanMonth.get(UserPlanMonth.field.EvaluatePeople.ToString(), UserPlanMonth.field.MonthPlanId.ToString(), _username);
                if (_iSEvaluatePeople.Rows.Count == 0 && objUserInfo.UserName != "admin")
                {
                    Utility.HttpHelp.alert("非评估人不能修改计划", Page);
                }
                else
                {
                    string _url = "UserMonthPlanModify.aspx?Id=" + _monthId + "&url=UserPlanMonthList.aspx&MonthDate=" + Utility.HttpHelp.getParamsInt("Id") + "&depart=" + department + "&Year=" + this.drpYear.SelectedValue + "&Quarter=" + this.drpQuarterDate.SelectedValue + "&Month=" + this.drpMonthDate.SelectedValue + "&User=" + this.drpUserInfo.SelectedValue;
                    Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=1000,top=300,left=20,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
                    return;
                }
            }

        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能修改一个月计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个月计划", Page);
            return;
        }
    }

    /// <summary>
    /// 修改持续计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyContinue_Click(object sender, EventArgs e)
    {
        BLL.UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
        string department = drpDepartment.SelectedItem.Value;
        object[] _valueMonth = new object[1];
        int _count = 0;
        int _monthId = 0;
        for (int i = 0; i < this.gdvContinueMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvContinueMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    object[] _value = new object[1];
                    _value[0] = _monthId;
                    DataTable _monthPlan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
                    if (_monthPlan.Rows.Count > 0)
                    {
                        if (objUserInfo.UserName != "admin")
                        {
                            _valueMonth[0] = _monthId;
                            object[] _user = new object[1];
                            _user[0] = objUserInfo.UserName;
                            DataTable _usertable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _user);
                            string RealName = "";
                            if (_usertable.Rows.Count > 0)
                            {
                                RealName = _usertable.Rows[0]["RealName"].ToString();
                            }

                            if (_monthPlan.Rows[0]["EvaluatePeople"].ToString() == RealName)
                            {
                                _count++;
                            }
                            else
                            {
                                int val = dllUserPlanMonth.IsUserMonthPlanResolve(_valueMonth);
                                if (val == 0)
                                {
                                    _count++;
                                }
                                else
                                {
                                    Utility.HttpHelp.alert("该月计划已经分解到周中", this.Page);
                                    return;
                                }
                            }
                        }
                        else
                        {
                            _count++;
                        }
                    }
                }
            }
        }
        if (_count == 1)
        {

            //判断是否计划二次评估完成， 评估完成不允许修改。
            object[] _value = new object[1];
            _value[0] = _monthId;
            DataTable monthplan = dllUserPlanMonth.get(UserPlanMonth.field.MonthPlanId.ToString(), _value);
            if (monthplan.Rows.Count == 0)
            {
                if (Convert.ToInt32(monthplan.Rows[0]["PlanSendState"]) >= 4)
                {
                    Utility.HttpHelp.alert("计划已二次评估完成，不允许修改！", Page);
                    return;
                }
            }

            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            BLL.UserInfoManager dllUserInfo = new UserInfoManager();
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
            if (_userTable.Rows.Count > 0)
            {
                object[] _username = new object[2];
                _username[0] = _userTable.Rows[0]["RealName"].ToString();
                _username[1] = _monthId;
                DataTable _iSEvaluatePeople = dllMonthPlan.get(MonthPlanManager.field.EvaluatePeople.ToString(), MonthPlanManager.field.MonthPlanId.ToString(), _username);
                if (_iSEvaluatePeople.Rows.Count == 0 && objUserInfo.UserName != "admin")
                {
                    Utility.HttpHelp.alert("非评估人不能修改计划", Page);
                }
                else
                {
                    string _url = "UserMonthPlanModify.aspx?Id=" + _monthId + "&url=UserPlanMonthList.aspx&MonthDate=" + Utility.HttpHelp.getParamsInt("Id") + "&depart=" + department + "&Year=" + this.drpYear.SelectedValue + "&Quarter=" + this.drpQuarterDate.SelectedValue + "&Month=" + this.drpMonthDate.SelectedValue + "&User=" + this.drpUserInfo.SelectedValue;
                    Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=1000,top=300,left=20,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
                    return;
                }
            }

        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能修改一个月计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个月计划", Page);
            return;
        }
    }


    /// <summary>
    /// 查看分解
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnMonthResolve_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int _monthId = 0;
        string _monthPlanSortId = string.Empty;
        for (int i = 0; i < this.gdvMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                if (ckb.Checked == true)
                {
                    if (this.gdvMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                    {
                        _monthId = Convert.ToInt32(this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString());
                        _monthPlanSortId = this.gdvMonthList.DataKeys[i]["MonthPlanSortId"].ToString();
                        _count++;
                    }
                }
            }
        }
        if (_count == 1)
        {
            string _url = "UserMonthPlanResolveAddForTips.aspx?_MonthPlanId=" + _monthId + "&_MonthPlanSortId=" + _monthPlanSortId;
            Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=1000,top=300,left=20,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            return;
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能修改一个月计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个月计划", Page);
            return;
        }
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSavePreMonthSummary_Click(object sender, EventArgs e)
    {
        if (this.drpDepartment.SelectedItem.Text != "请选择部门")
        {
            if (sender.Equals(this.btnSaveMonthSummary))
            {
                if (this.txtMonthSummary.Text.Length < 60)
                {
                    Utility.HttpHelp.alert("字数不得少于60字！", this.Page);
                    return;
                }
            }
            else
            {
                if (this.txtPreMonthSummary.Text.Length < 60)
                {
                    Utility.HttpHelp.alert("字数不得少于60字！", this.Page);
                    return;
                }
            }
            if (this.HMonthSummaryId.Value.Length > 0)
            { 
                //修改
                object[] _value = new object[7];
                _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
                _value[1] = this.txtPreMonthSummary.Text;
                _value[2] = this.txtMonthSummary.Text;
                _value[3] = objUserInfo.UserName;
                BLL.UserInfoManager dllUserInfo = new BLL.UserInfoManager();
                DataTable dt = dllUserInfo.get(BLL.UserInfoManager.field.UserName.ToString(), new object[] { objUserInfo.UserName });
                _value[4] = Convert.ToInt32(dt.Rows[0]["UserId"]);
                _value[5] = this.drpDepartment.SelectedItem.Text;
                _value[6] = Convert.ToInt32(this.HMonthSummaryId.Value);
                int val = new BLL.UserMonthSummary().editMonthSummary(_value);
                if (val > 0)
                {
                    bindGridView(this.gdvPreMonthList);
                }
                else
                {
                    Utility.HttpHelp.alert("修改失败", this.Page);
                }
            }
            else
            {

                //添加
                object[] _value = new object[6];
                _value[0] = Convert.ToInt32(this.drpMonthDate.SelectedValue);
                _value[1] = this.txtPreMonthSummary.Text;
                _value[2] = this.txtMonthSummary.Text;
                _value[3] = objUserInfo.UserName;
                _value[4] = this.drpDepartment.SelectedItem.Text;
                _value[5] = Convert.ToInt32(this.drpUserInfo.SelectedValue);
                int val = new BLL.UserMonthSummary().add(_value);
                if (val > 0)
                {
                    bindGridView(this.gdvPreMonthList);
                }
                else
                {
                    Utility.HttpHelp.alert("添加失败", this.Page);
                }
            }
            if (sender.Equals(this.btnSaveMonthSummary))
            {
                this.btnAddMonthSummary.Visible = true;
                this.btnSaveMonthSummary.Visible = false;
                this.txtMonthSummary.ReadOnly = true;
                //this.lblMonthSummary.Visible = true;
            }
            if (sender.Equals(this.btnSavePreMonthSummary))
            {
                this.btnAddPreMonthSummary.Visible = true;
                this.btnSavePreMonthSummary.Visible = false;
                this.txtPreMonthSummary.ReadOnly = true;
                //this.lblPreMonthSummary.Visible = true;
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择部门", this.Page);
        }
    }


    /// <summary>
    /// 下载事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownMonthSummary_Click(object sender, EventArgs e)
    {
        if (sender.Equals(this.btnDownMonthSummary))
        {
            string path = this.HAttachmentPath.Value;
            FileDownload(path);
        }
        else
        {
            string path = this.HPreAttachmentPath.Value;
            FileDownload(path);
        }
    }


    /// <summary>
    /// 流程查看
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFlow_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int _monthId = 0;
        int type = -1;//1 上月工作情况 2 本月工作情况 3上月未完成工作  4 持续进行工作
        //上月工作情况
        for (int i = 0; i < this.gdvPreMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvPreMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvPreMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvPreMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvPreMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    _count++;
                    type = 1;
                }
            }
        }
        //本月工作情况
        for (int i = 0; i < this.gdvMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    _count++;
                    type = 2;
                }
            }
        }
        //上月未完成工作
        for (int i = 0; i < this.gdvPreNoCompeleteMonthPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvPreNoCompeleteMonthPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"] != null && this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvPreNoCompeleteMonthPlan.DataKeys[i]["MonthPlanId"].ToString());
                    _count++;
                    type = 3;
                }
            }
        }
        //持续进行工作
        for (int i = 0; i < this.gdvContinueMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvContinueMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"] != null && this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"].ToString().Length != 0)
                {
                    _monthId = Convert.ToInt32(this.gdvContinueMonthList.DataKeys[i]["MonthPlanId"].ToString());
                    _count++;
                    type = 4;
                }
            }
        }
        if (_count == 1)
        {
            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            string _url = "../WebCommon/VerifyCircruitForUserPlanDisplay.aspx?PlanId=" + _monthId + "&PlanType=4";
            Page.RegisterStartupScript("windowOpen",
               "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=150,width=550,top=250,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");

        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能查看一个月计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个月计划", Page);
            return;
        }
    }


    /// <summary>
    /// 导出excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnToExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[4];
        _gridView[0] = this.gdvPreMonthList;
        _gridView[1] = this.gdvMonthList;
        _gridView[2] = this.gdvPreNoCompeleteMonthPlan;
        _gridView[3] = this.gdvContinueMonthList;
        Utility.Comm.UserMonthToExcel(GridViewToDataTable(_gridView), this.drpDepartment.SelectedItem.Text, this.drpUserInfo.SelectedItem.Text, this.drpMonthDate.SelectedItem.Text);
    }

    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < _sourceGridView[i].Columns.Count; j++)
            {
                if (_sourceGridView[i].Columns[j].Visible == true)
                {
                    dc = new DataColumn(_sourceGridView[i].Columns[j].HeaderText);
                    _targetDataTable[i].Columns.Add(dc);
                }
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < _sourceGridView[i].Columns.Count; k++)
                {

                    if (_sourceGridView[i].Columns[k].Visible == true && k < _targetDataTable[i].Columns.Count)
                    {
                        if (_sourceGridView[i].Columns[k].HeaderText == "计划来源")
                        {
                            LinkButton lbtn = (LinkButton)_sourceGridView[i].Rows[j].Cells[k].FindControl("lbtnSource");
                            dr[k] = lbtn.Text;
                        }
                        else
                        {
                            dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;
                        }

                    }

                }
                _targetDataTable[i].Rows.Add(dr);
            }

        }
        return _targetDataTable;
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        List<object[]> _valueList = new List<object[]>();
        int _count = 0;
        for (int i = 0; i < this.gdvMonthList.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvMonthList.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.gdvMonthList.DataKeys[i]["MonthPlanId"].ToString());
                _valueList.Add(_value);
            }
        }
        if (_count <= 0)
        {
            Utility.HttpHelp.alert("选择要删除的计划", Page);
            return;
        }
        if (_valueList.Count > 0)
        {
            BLL.UserPlanMonth dllUserPlanMonth = new UserPlanMonth();
            int _result = dllUserPlanMonth.delWithTransaction(UserPlanMonth.field.MonthPlanId.ToString(), _valueList);
            if (_result == 0)
            {
                Utility.HttpHelp.redirectPage("删除计划未成功，请重试");
            }
            else
            {
                bindGridView(this.gdvMonthList);
            }
        }
    }



    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string department = drpDepartment.SelectedItem.Value;
        string _height = this.h1h.Value;
        string _width = this.h2w.Value;
        string _url = "UserPlanMonthListDisPlay.aspx?Id=" + Convert.ToInt32(this.drpMonthDate.SelectedValue) + "&depart=" + department + "&Year=" + this.drpYear.SelectedValue + "&QuarterDate=" + this.drpQuarterDate.SelectedValue + "&MonthDate=" + this.drpMonthDate.SelectedValue + "&User=" + this.drpUserInfo.SelectedValue;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height= " + _height + ",width=" + _width + ",top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }
}
